<?php
include("include/config.inc.php");

$pageName = 'billList';
if($_SESSION['s_userType'] == 'User')
{
   $authorization = "NoAccess";
   foreach($selUsermenuArr as $value)
   {
     if(in_array($pageName, $value))
       {
         $authorization ="Access";
         break;
       }
       else 
       {
        $authorization = "NoAccess";    
       }
   }
   if($authorization == "NoAccess")
   {
     header("location:index.php");
   }
}
if(!isset($_SESSION['s_activId']))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{	
	$billMasterArr = 0;
  $masterArray = array();
  $detailArray = array();
	
	if(isset($_POST['updateBtn']) && $_POST['billMasterId'] > 0)
  {
    $partyId          = isset($_POST['partyId']) ? $_POST['partyId'] : 0;
    $billNo           = (isset($_POST['billNo'])) && ($_POST['billNo'] != "") ? $_POST['billNo'] : 0 ;
  	$date             = $_POST['billDateYear']."-".$_POST['billDateMonth']."-".$_POST['billDateDay'];
  	$outDate          = $_POST['outDateYear']."-".$_POST['outDateMonth']."-".$_POST['outDateDay'];
  	$outChallenNo     = (isset($_POST['outChallenNo'])) && ($_POST['outChallenNo'] != "") ? $_POST['outChallenNo'] : 0 ;
  	$inChallenNo      = (isset($_POST['inChallenNo'])) && ($_POST['inChallenNo'] != "") ? $_POST['inChallenNo'] : 0 ;
  	$materialId       = (isset($_POST['materialId'])) && ($_POST['materialId'] != "") ? $_POST['materialId'] : 0 ;
  	$finalProcess     = (isset($_POST['finalProcess'])) && ($_POST['finalProcess'] != "") ? $_POST['finalProcess'] : 0 ;
  	$outQuantity      = (isset($_POST['outQuantity'])) && ($_POST['outQuantity'] != "") ? $_POST['outQuantity'] : 0 ;
  	$outWieght        = (isset($_POST['outWieght'])) && ($_POST['outWieght'] != "") ? $_POST['outWieght'] : 0 ;
  	$outRate          = (isset($_POST['outRate'])) && ($_POST['outRate'] != "") ? $_POST['outRate'] : 0 ;
  	$amount           = (isset($_POST['amount'])) && ($_POST['amount'] != "") ? $_POST['amount'] : 0 ;
  	$total            = (isset($_POST['total'])) && ($_POST['total'] != "") ? $_POST['total'] : 0 ;
  	$taxInPrecentage  = (isset($_POST['taxInPrecentage'])) && ($_POST['taxInPrecentage'] != "") ? $_POST['taxInPrecentage'] : 0 ;
  	$serviceTax       = (isset($_POST['serviceTax'])) && ($_POST['serviceTax'] != "") ? $_POST['serviceTax'] : 0 ;
  	$roundOff         = (isset($_POST['roundOff'])) && ($_POST['roundOff'] != "") ? $_POST['roundOff'] : 0 ;
  	$netTotal         = (isset($_POST['netTotal'])) && ($_POST['netTotal'] != "") ? $_POST['netTotal'] : 0 ;
  
 echo   $updateIncoming = "UPDATE billmaster
                          SET partyId = '".$partyId."',
                              billNo = '".$billNo."',
                              date = '".$date."' ,
                              total = '".$total."',
                              taxInPrecentage = '".$taxInPrecentage."',
                              serviceTax = '".$serviceTax."',
                              roundOff = '".$roundOff."',
                              netTotal = '".$netTotal."'
                        WHERE billMasterId = ".$_POST['billMasterId'];                         			  
    $updateIncomingResult = mysql_query($updateIncoming);
    if(!$updateIncomingResult)
    {
      die("Update Query Not Updated for Master Table : ".mysql_error());
    }
    else
    {
     echo $updateDetail = "UPDATE billdetail
                                SET finalProcess = '".$finalProcess."',
                                    outQuantity = '".$outQuantity."',
                                    outWieght = '".$outWieght."',
                                    outRate = '".$outRate."',
                                    amount = '".$amount."'
                        WHERE billMasterId = ".$_POST['billMasterId'];                         			  
    $updateDetailResult = mysql_query($updateDetail); 
      
                        
    }
  }
	
	if(isset($_GET['detail']) && isset($_GET['billMasterId']) && $_GET['billMasterId'] > 0)
	{
 	  $selBillMaster = "SELECT billMasterId, billNo, DATE_FORMAT(date,'%d-%m-%Y') AS billDate, billmaster.partyId, total, taxInPrecentage, serviceTax, roundOff, netTotal, notes
		                    FROM billmaster
		                    JOIN party ON billmaster.partyId = party.partyId
		                   WHERE billMasterId = ".$_GET['billMasterId'];
    $selBillMasterRes = mysql_query($selBillMaster);
    while($mRow = mysql_fetch_array($selBillMasterRes))
    {
      $masterArray['billMasterId']    = $mRow['billMasterId'];
      $masterArray['billNo']          = $mRow['billNo'];
      $masterArray['billDate']        = $mRow['billDate'];
      $masterArray['partyId']         = $mRow['partyId'];
      $masterArray['total']           = $mRow['total'];
      $masterArray['taxInPrecentage'] = $mRow['taxInPrecentage'];
      $masterArray['serviceTax']      = $mRow['serviceTax'];
      $masterArray['roundOff']        = $mRow['roundOff'];
      $masterArray['netTotal']        = $mRow['netTotal'];
      $masterArray['notes']           = $mRow['notes'];
      
      $selBillDetail = "SELECT billdetail.*, DATE_FORMAT(outgoingmaster.date,'%d-%m-%y') AS outDate,outgoingmaster.date,  outChallenNo, incoming.challan AS inChallenNo,materialName,incoming.materialId,billdetail.finalProcess
                          FROM billdetail
                          JOIN outgoingmaster ON billdetail.outGoingMasterId = outgoingmaster.outGoingMasterId
                          JOIN incoming ON outgoingmaster.incomingId = incoming.incomingId
                          JOIN material ON incoming.materialId = material.materialId
                         WHERE billMasterId = ".$mRow['billMasterId'];
      $selBillDetailRes = mysql_query($selBillDetail);
      $dCount = 0;
      while($dRow = mysql_fetch_array($selBillDetailRes))
      {
      	$detailArray[$dCount]['date']         = $dRow['date'];
      	$detailArray[$dCount]['outDate']      = $dRow['outDate'];
      	$detailArray[$dCount]['outChallenNo'] = $dRow['outChallenNo'];
      	$detailArray[$dCount]['inChallenNo']  = $dRow['inChallenNo'];
      	$detailArray[$dCount]['materialId']   = $dRow['materialId'];
      	$detailArray[$dCount]['materialName'] = $dRow['materialName'];
      	$detailArray[$dCount]['finalProcess'] = $dRow['finalProcess'];
      	$detailArray[$dCount]['outQuantity']  = $dRow['outQuantity'];
      	$detailArray[$dCount]['outWieght']    = $dRow['outWieght'];
      	$detailArray[$dCount]['outRate']      = $dRow['outRate'];
      	$detailArray[$dCount]['amount']       = $dRow['amount'];
      	$dCount++;
      }
    }
	}
	
	if(isset($_POST['partyId']) && $_POST['partyId'] > 0)
	{
		$selectBill = "SELECT billMasterId, billNo, DATE_FORMAT(date,'%d-%m-%Y') AS billDate, partyName, total, serviceTax, roundOff, netTotal, notes
		                 FROM billmaster
		                 JOIN party ON billmaster.partyId = party.partyId
		                WHERE billmaster.partyId = ".$_POST['partyId'];
    $selectBillRes = mysql_query($selectBill);
    $billMasterArr = array();
    $cnt = 0;
    while($billRow = mysql_fetch_array($selectBillRes))
    {
    	$billMasterArr[$cnt]['billMasterId'] = $billRow['billMasterId'];
    	$billMasterArr[$cnt]['billNo']       = $billRow['billNo'];
    	$billMasterArr[$cnt]['billDate']     = $billRow['billDate'];
    	$billMasterArr[$cnt]['partyName']    = $billRow['partyName'];
    	$billMasterArr[$cnt]['total']        = $billRow['total'];
    	$billMasterArr[$cnt]['serviceTax']   = $billRow['serviceTax'];
    	$billMasterArr[$cnt]['roundOff']     = $billRow['roundOff'];
    	$billMasterArr[$cnt]['netTotal']     = $billRow['netTotal'];
    	$billMasterArr[$cnt]['notes']        = $billRow['notes'];
    	$cnt++;
    }
	}
	
  $k = 0;
  $partyArray = array();
  $partyId     = isset($_REQUEST['partyId']) ? $_REQUEST['partyId'] : 0;
  $selectParty = "SELECT partyId,partyName
                    FROM party
                   WHERE partyId IN (SELECT partyId FROM billmaster)
                   ORDER BY partyName";
  $selectPartyResult = mysql_query($selectParty);
  while($partyRow = mysql_fetch_array($selectPartyResult))
  {
    $partyArray['partyId'][$k]   = $partyRow['partyId'];
    $partyArray['partyName'][$k] = $partyRow['partyName'];
    $k++;
  }
  
  $m = 0;
  $catmaterialArray = array();
  $select = "SELECT materialId,materialName                                    
               FROM material
              ORDER BY materialName";                                                   
	$selectResult = mysql_query($select);                                       
	while($row = mysql_fetch_array($selectResult))                              
	{                                                                           
	  $catmaterialArray['materialId'][$m]   = $row['materialId'];                    
	  $catmaterialArray['materialName'][$m] = $row['materialName'];                      
	  $m++;                                                                     
	}
	
	
	$r = 0;
	$catprocessArray = array();
	$select = "SELECT finalProcessId,finalProcessName                                    
	             FROM finalprocess";                                                   
	$selectResult = mysql_query($select);                                       
	while($row = mysql_fetch_array($selectResult))                              
	{                                                                           
	  $catprocessArray['finalProcessId'][$r]   = $row['finalProcessId'];                    
	  $catprocessArray['finalProcessName'][$r] = $row['finalProcessName'];                      
	  $r++;                                                                     
	}    
	
  
  include("./bottom.php");
  $smarty->assign("billMasterArr",$billMasterArr);
  $smarty->assign("partyId",$partyId);
  $smarty->assign("partyArray",$partyArray);
  $smarty->assign("catmaterialArray",$catmaterialArray);
  $smarty->assign("catprocessArray",$catprocessArray);
  
  $smarty->assign("masterArray",$masterArray);
  $smarty->assign("detailArray",$detailArray);

  if(isset($_GET['detail']))
  {
    $smarty->display("billListEdit.tpl");
  }
  else
  {
    $smarty->display("billList.tpl");
  }
}
?>